package com.gjun.struts.resource.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gjun.struts.bean.ext.Book;
import com.gjun.struts.resource.dao.IBookDao;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class BookDaoImpl extends HibernateDaoSupport implements IBookDao {
	
	private static final Logger log = LoggerFactory.getLogger(IBookDao.class);

	public List<Book> findNewBook() {
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "from Book o order by o.addTime";
		List<Book> books = getHibernateTemplate().find(hql); 
		session.close();
		return books;
	}

	public List<Book> findRecommendBook() {
		String hql = "from Book o order by o.addTime";
		List<Book> books = getHibernateTemplate().find(hql); 
		return books;
	}

	public Book getBookById(int id) {
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "select b from Book b where b.id=?";
		Book book = (Book) session.createQuery(hql).setParameter(0, id).uniqueResult();
		book.getCats().size();
		session.close();
		return book;
	}

	public void saveBook(final Book book) {
		this.getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				
				return session.save(book);
			}
		});
		
	}

	public int getLastId() {
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "select max(b.id) from Book b";
		List list = session.createQuery(hql).list();
		session.close();
		return (Integer)list.get(0);
	}

	public List<Book> findBookByConditions(final String conditions) {
		// TODO Auto-generated method stub
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "select b from Book b where b.hasDeleted=0 " + conditions;
		List list = session.createQuery(hql).list();
		session.close();
		return list;
	}
	
	public List<Book> findBookByConditions(final String conditions,int start,int max) {
		// TODO Auto-generated method stub
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "select b from Book b where b.hasDeleted=0 " + conditions;
//		if(conditions.isEmpty()){
//			hql = "select b from Book b";
//		}
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(max);
		List<Book> list = query.list();
		for (Book book : list) {
			System.out.println(book.getCats().size());
		}
		session.close();
		return list;
	}

	public int delBooksById(int id) {
		// TODO Auto-generated method stub
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "update Book b set b.hasDeleted=1 where b.id=?";
		session.beginTransaction();
		Query query = session.createQuery(hql);
		
		query.setParameter(0, id);
		int i = query.executeUpdate();
		session.getTransaction().commit();
		session.close();
		return i;
	}

	public void delBooks(Book book) {
		// TODO Auto-generated method stub
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		session.beginTransaction();
		session.update(book);
		session.getTransaction().commit();
		session.close();
	}

	public void updateBook(Book book) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(book);
	}
		
	

}
